package stu.khy.action;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import stu.khy.comm.Constants;
import stu.khy.entity.ClaimVoucher;
import stu.khy.entity.ClaimVoucherDetail;
import stu.khy.entity.Employee;
import stu.khy.service.ClaimVoucherService;

public class ClaimVoucherAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private ClaimVoucher claimVoucher;// 报销单
	private List<ClaimVoucherDetail> detailList;// 报销单明细

	@Resource
	private ClaimVoucherService claimVoucherService;

	private String status;
	private Date startDate;
	private Date endDate;
	private static Map<String, String> statusMap;
	static {
		statusMap = new LinkedHashMap<String, String>();
		statusMap.put(Constants.CLAIMVOUCHER_CREATED, "新创建");
		statusMap.put(Constants.CLAIMVOUCHER_SUBMITTED, "已提交");
		statusMap.put(Constants.CLAIMVOUCHER_APPROVING, "待审核");
		statusMap.put(Constants.CLAIMVOUCHER_BACK, "已打回");
		statusMap.put(Constants.CLAIMVOUCHER_APPROVED, "已审批");
		statusMap.put(Constants.CLAIMVOUCHER_RAID, "已付款");
		statusMap.put(Constants.CLAIMVOUCHER_TERMINATED, "已终止");

	}

	// 添加
	public String saveCLaimVoucher() {
		claimVoucher.setCreator(this.getLoginEmployee());// 绑定登录用户
		if (claimVoucher.getStatus().equals(Constants.CLAIMVOUCHER_SUBMITTED)) {
			claimVoucher.setNextDeal((Employee) this.getSession().get(Constants.AUTH_EMPLOYEE_MANAGER));

		} else {
			claimVoucher.setNextDeal(claimVoucher.getCreator());
		}
		claimVoucher.setDetailList(detailList); // 进行绑定
		for (ClaimVoucherDetail d : detailList) {
			d.setBizClaimVoucher(claimVoucher);
		}
		claimVoucherService.saveClaimVoucher(claimVoucher); // 使用重定向
		return "redirectList";

	}

	public String searchClaimVoucher() {
		pageSupport = claimVoucherService.getClaimVoucherPage(this.getLoginEmployee().getSn(), status, startDate,
				endDate, pageNo, pageSize);

		return "list";
	}

	public String getClaimVoucherById() {
		claimVoucher = claimVoucherService.findClaimVoucherById(claimVoucher.getId());
		return "view";

	}

	// 报销单明细
	public String toUpdate() {
		claimVoucher = claimVoucherService.findClaimVoucherById(claimVoucher.getId());
		return "update";

	}

	// 更新
	public String updateClaimVoucher() {
		// 在配置文件字段中加入update="false"表示不参与更新操作
		if (claimVoucher.getStatus().equals(Constants.CLAIMVOUCHER_SUBMITTED)) {
			claimVoucher.setNextDeal((Employee) this.getSession().get(Constants.AUTH_EMPLOYEE_MANAGER));

		} else {
			//下一步处理人
			claimVoucher.setNextDeal(this.getLoginEmployee());
		}
		//重新绑定明细，删除旧的明细  避免对一些不能更改的元素进行更改
		claimVoucher.setDetailList(detailList); // 进行绑定
		for (ClaimVoucherDetail d : detailList) {
			d.setBizClaimVoucher(claimVoucher);
		}
		return "list";
	}

	public ClaimVoucher getClaimVoucher() {
		return claimVoucher;
	}

	public void setClaimVoucher(ClaimVoucher claimVoucher) {
		this.claimVoucher = claimVoucher;
	}

	public List<ClaimVoucherDetail> getDetailList() {
		return detailList;
	}

	public void setDetailList(List<ClaimVoucherDetail> detailList) {
		this.detailList = detailList;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public static Map<String, String> getStatusMap() {
		return statusMap;
	}

	public static void setStatusMap(Map<String, String> statusMap) {
		ClaimVoucherAction.statusMap = statusMap;
	}

}
